package juc;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 任务并行
 */
public class CompleteableFutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> future1 =
                CompletableFuture.supplyAsync(() -> {
                    System.out.println("我是1");
                    return 1;
                });

        CompletableFuture<Integer> future2 =
                CompletableFuture.supplyAsync(() -> {
                    System.out.println("我是2");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return 2;
                });
        CompletableFuture<Integer> future3 =
                CompletableFuture.supplyAsync(() -> {
                    System.out.println("我是3");
                    return 3;
                });
        CompletableFuture<Integer> future4 =
                CompletableFuture.supplyAsync(() -> {
                    System.out.println("我是4");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return 0;
                });
        CompletableFuture<Integer> future5 =
                CompletableFuture.supplyAsync(() -> {
                    System.out.println("我是5");
                    return 5;
                });
        // 必须等待所有的future全部完成才可以 (一起成功或失败)
//        CompletableFuture.allOf(future1, future2,future3,future4,future5).join();
//        Integer integer1 = future1.get();
//        Integer integer2 = future2.get();
//        Integer integer3 = future3.get();
//        Integer integer4 = future4.get();
//        Integer integer5 = future5.get();
        Integer integer1 = future1.join();
        Integer integer2 = future2.join();
        Integer integer3 = future3.join();
        Integer integer4 = future4.join();
        Integer integer5 = future5.join();
//        Integer integer1 = future1.join();
//        Integer integer2 = future2.join();
        System.out.println("integer1: " + integer1);
        System.out.println("integer2: " + integer2);
        System.out.println("integer2: " + integer3);
        System.out.println("integer2: " + integer4);
        System.out.println("integer2: " + integer5);

        System.out.println("ssss");

//        Stream.of(future1,future2).map(CompletableFuture::join).forEach(System.out::println);


        // 只要有一个future完成，就执行后续的代码
//        ArrayList<CompletableFuture<Integer>> list = new ArrayList<>();
//        list.add(future1);
//        list.add(future2);
//        list.add(future3);
//        list.add(future4);
//        list.add(future5);
//        list.parallelStream().map(CompletableFuture::join).forEach(System.out::println);//结果 3 1 4 5 2 异步
//        list.stream().map(CompletableFuture::join).forEach(System.out::println);//结果 1 2 3 4 5 非异步





    }

}
